﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SchoolCardSystem
{
    public partial class InPrice : System.Web.UI.Page
    {
        //定义数据库信息
        //connection=查询 connection2=更新 connection3=插入
        MySqlConnection connection = new MySqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["connection"].ConnectionString.ToString());
        MySqlConnection connection2 = new MySqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["connection"].ConnectionString.ToString());
        MySqlConnection connection3 = new MySqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["connection"].ConnectionString.ToString());
        //定义返回的余额和卡号
        String getPrice;
        String userId;
        String isNum = @"^(\d{1,3}|1000)$";
        protected void Page_Load(object sender, EventArgs e)
        {
            //隐藏提示框
            InPriceSucc.Style["display"] = "none";
            //打开数据库
            connection.Open();
            //返回主键
            userId = Session["name"].ToString();
            //查询语句
            string sqlstr1 = @"select cardPrice from card where cardId=@userId";
            //Command 查询语句
            MySqlCommand select = new MySqlCommand(sqlstr1, connection);
            //设置@userId的类型
            select.Parameters.Add(new MySqlParameter("@userId", MySqlDbType.VarChar, 11));
            select.Parameters["@userId"].Value = userId;
            //在线模式
            MySqlDataReader selectdr = select.ExecuteReader();
            if (!selectdr.Read())
            {
                InPriceSucc.Text = "无法读取用户信息";
            }
            //查询成功
            else
            {
                getPrice = selectdr["cardPrice"].ToString();
            }
            connection.Close();
        }

        protected void InPriceBtn_Click(object sender, EventArgs e)
        {
            //用户要充值金额
            if (InPriceText.Text.Trim() == "")
            {
                InPriceSucc.Style["display"] = "inline";
                InPriceSucc.Text = "请输入充值金额";
                InPriceText.Focus();
            }else if(Regex.IsMatch(InPriceText.Text.Trim(),isNum)==false)//验证功能
            {
                InPriceSucc.Style["display"] = "inline";
                InPriceSucc.Text = "请输入0-1000的数字";
                InPriceText.Focus();
            }
            else 
            {
                //获取用户输入的充值金额
                String priceStr = InPriceText.Text;
                double price = 0;
                try
                {
                    price = double.Parse(priceStr);
                }
                catch (Exception error)
                {
                    Console.Write("{0}", error.Message);
                }
                //更新语句
                string sqlstr2 = @"update card set cardPrice=@sumPrice where cardId=@userId";
                //Command 更新语句
                MySqlCommand update = new MySqlCommand(sqlstr2, connection2);

                //设置@price/@userId的类型
                update.Parameters.Add(new MySqlParameter("@sumPrice",MySqlDbType.Double,9));
                update.Parameters.Add(new MySqlParameter("@userId", MySqlDbType.VarChar, 11));
                update.Parameters["@userId"].Value = userId;
                //充值后金额
                double sumPrice = price + double.Parse(getPrice);
                //定义command 更新后余额
                update.Parameters["@sumPrice"].Value = sumPrice;
                //成功后显示提示框
                InPriceSucc.Style["display"] = "inline";
                InPriceSucc.Text = "充值成功";
                //打开数据库
                connection2.Open();
                //执行语句
                update.ExecuteNonQuery();
                connection2.Close();

                //在数据库中插入充值记录
                string sqlstr3 = @"insert into cardrecord values (@userId,@InPrice,@nowTime,null,null)";
                MySqlCommand insert = new MySqlCommand(sqlstr3, connection3);
                //定义金额
                insert.Parameters.Add(new MySqlParameter("@InPrice", MySqlDbType.Double, 9));
                insert.Parameters["@InPrice"].Value = price;
                //定义卡号
                insert.Parameters.Add(new MySqlParameter("@userId",MySqlDbType.VarChar,11));
                insert.Parameters["@userId"].Value = userId;
                //定义时间
                insert.Parameters.Add(new MySqlParameter("@nowTime", MySqlDbType.DateTime));
                insert.Parameters["@nowTime"].Value = DateTime.Now.ToString();
                connection3.Open();
                insert.ExecuteNonQuery();
                connection3.Close();
                
            }
        }
    }
}